package com.whty.mongodemo.service;

import com.whty.mongodemo.constant.EventBusAddress;
import com.whty.mongodemo.constant.ParamKey;
import com.whty.mongodemo.holder.VertxHolder;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;

public class BaseService {

  private final Vertx vertx = VertxHolder.getVertx();

  private final EventBus eventBus = vertx.eventBus();

  private final String currentCollection;

  public BaseService(String collection) {
    this.currentCollection = collection;
  }

  public String getCurrentCollection() {
    return currentCollection;
  }

  public void queryById(String id, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject query = new JsonObject().put("_id", id);
    JsonObject message = new JsonObject().put(ParamKey.COLLECTION, currentCollection).put(ParamKey.QUERY, query);
    eventBus.request(EventBusAddress.QUERY_BY_ID, message, replyHandler);
  }

  public void query(JsonObject query, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject().put(ParamKey.COLLECTION, currentCollection);
    if (null != query) {
      message.put(ParamKey.QUERY, query);
    }
    eventBus.request(EventBusAddress.QUERY, message, replyHandler);
  }

  public void queryByPage(JsonObject page, JsonObject query, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject()
      .put(ParamKey.COLLECTION, currentCollection)
      .put(ParamKey.PAGE, page)
      .put(ParamKey.SORT, new JsonObject().put("_id", -1));
    if (null != query) {
      message.put(ParamKey.QUERY, query);
    }
    eventBus.request(EventBusAddress.QUERY_BY_PAGE, message, replyHandler);
  }

  public void queryWithLookup(JsonObject lookup, JsonObject query, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject()
      .put(ParamKey.COLLECTION, currentCollection)
      .put("lookup", lookup);
    if (null != query) {
      message.put(ParamKey.QUERY, query);
    }
    eventBus.request(EventBusAddress.QUERY_WITH_LOOKUP, message, replyHandler);
  }

  public void insert(JsonObject query, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject().put(ParamKey.COLLECTION, currentCollection).put(ParamKey.QUERY, query);
    eventBus.request(EventBusAddress.INSERT, message, replyHandler);
  }

  public void updateById(String id, JsonObject data, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject()
      .put(ParamKey.COLLECTION, currentCollection)
      .put(ParamKey.QUERY, new JsonObject().put("_id", id));
    message.put(ParamKey.DATA, data);
    eventBus.request(EventBusAddress.UPDATE, message, replyHandler);
  }

  public void removeById(String id, Handler<AsyncResult<Message<Object>>> replyHandler) {
    JsonObject message = new JsonObject()
      .put(ParamKey.SINGLE, true)
      .put(ParamKey.COLLECTION, currentCollection)
      .put(ParamKey.QUERY, new JsonObject().put("_id", id));
    eventBus.request(EventBusAddress.REMOVE, message, replyHandler);
  }
}
